package com.fly.config;

import com.fly.handler.DepartmentHandler;
import com.fly.handler.MenuHandler;
import com.fly.handler.RoleHandler;
import com.fly.handler.UserHandler;
import com.fly.vo.Response;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.server.RequestPredicates.DELETE;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

/**
 * @author Fly
 * @Description
 * @Date Created in 10:56 2018/1/24
 * @Modified by
 */
@Configuration
public class RouterFunctionConfig {

    @Bean
    public RouterFunction<ServerResponse> loginRouterFunction(){
        return route(GET("/"), serverRequest -> {
            Response response = new Response(true,"登录成功",null);
            return ServerResponse.ok().body(fromObject(response));
        }).andRoute(GET("/loginFail"), serverRequest -> {
            String message = serverRequest.queryParam("message").get();
            Response response = new Response(false,message,null);
            return ServerResponse.ok().body(fromObject(response));
        });
    }
    /**
     * 菜单路由
     * @param menuHandler
     * @return
     */
    @Bean
    public RouterFunction<ServerResponse> menuRouterFunction(MenuHandler menuHandler){
        return route(GET("/menus"),menuHandler::list)
                .andRoute(POST("/menus"),menuHandler::save)
                .andRoute(GET("/menus/tree/{parentId}"),menuHandler::getByParentId)
                .andRoute(GET("/menus/rTree/{roleId}"),menuHandler::getByRoleId)
                .andRoute(GET("/menus/current"),menuHandler::currentMenu);
    }

    /**
     * 用户管理路由
     * @param userHandler
     * @return
     */
    @Bean
    public RouterFunction<ServerResponse> userRouterFunction(UserHandler userHandler){
        return route(GET("/users"),userHandler::list)
                .andRoute(POST("/users"),userHandler::save)
                .andRoute(DELETE("/users/{ids}"),userHandler::delete);
    }

    /**
     * 部门管理路由
     * @param departmentHandler
     * @return
     */
    @Bean
    public RouterFunction<ServerResponse> departmentRouterFunction(DepartmentHandler departmentHandler){
        return route(GET("/departments"),departmentHandler::list)
                .andRoute(POST("/departments"),departmentHandler::save)
                .andRoute(DELETE("/departments/{id}"),departmentHandler::remove)
                .andRoute(GET("/departments/{id}"),departmentHandler::getDepartment)
                .andRoute(GET("/departments/tree/{id}"),departmentHandler::getTreeData)
                .andRoute(GET("/departments/filter/{name}"),departmentHandler::filterData);
    }


    /**
     * 权限管理路由
     * @param roleHandler
     * @return
     */
    @Bean
    public RouterFunction<ServerResponse> roleRouterFunction(RoleHandler roleHandler){
        return route(GET("/roles"),roleHandler::list)
                .andRoute(POST("/roles"),roleHandler::save)
                .andRoute(DELETE("/roles/{id}"),roleHandler::remove);
    }
}
